package cn.edu.gduf.luowowo.article.service.impl;

import cn.edu.gduf.luowowo.article.domain.Travel;
import cn.edu.gduf.luowowo.article.domain.TravelContent;
import cn.edu.gduf.luowowo.article.mapper.TravelContentMapper;
import cn.edu.gduf.luowowo.article.mapper.TravelMapper;
import cn.edu.gduf.luowowo.article.query.TravelQuery;
import cn.edu.gduf.luowowo.article.service.ITravelService;
import cn.edu.gduf.luowowo.common.exception.LogicException;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class TravelServiceImpl implements ITravelService {
    @Autowired
    private TravelMapper travelMapper;
    @Autowired
    private TravelContentMapper contentMapper;

    @Override
    public PageInfo query(TravelQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        return new PageInfo(travelMapper.selectForList(qo));
    }

    @Override
    public Travel get(Long id) {
        return travelMapper.selectByPrimaryKey(id);
    }

    @Override
    public TravelContent getContent(Long id) {
        return contentMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Travel> getViewNumTop3(Long destId) {
        return travelMapper.selectViewNumTop3(destId);
    }

    @Override
    public Long saveOrUpdate(Travel travel) throws LogicException {
        //最新更新时间
        travel.setLastUpdateTime(new Date());
        //游记摘要
        TravelContent travelContent = travel.getTravelContent();
        if (travelContent == null){
            throw new LogicException("游记内容不能为空");
        }
        if (travelContent.getContent().length() > 200) {
            travel.setSummary(travelContent.getContent().substring(0, 200));
        } else {
            travel.setSummary(travelContent.getContent());
        }
        //游记内容
        if (travel.getId() == null) { //添加
            //创建时间
            travel.setCreateTime(new Date());
            travelMapper.insert(travel);
            travelContent.setId(travel.getId());
            contentMapper.insert(travelContent);
        } else { //更新
            //判断当前用户修改的是否为自己的游记
            Long currentId = travel.getAuthor().getId(); //当前用户的id
            //查询属于该用户的所有游记id
            List<Long> travelIds = new ArrayList<>();
            travelIds= travelMapper.selectByUserId(currentId);
            if (!travelIds.contains(travel.getId())){
                throw new LogicException("非法操作，不可修改他人的游记");
            }
            travelMapper.updateByPrimaryKey(travel);
            travelContent.setId(travel.getId());
            contentMapper.updateByPrimaryKey(travelContent);
        }
        return travel.getId();
    }

    @Override
    public List<Travel> list() {
        return travelMapper.selectAll();
    }

    @Override
    public void changeState(Travel travel) {
        if (travel.getState() == Travel.STATE_RELEASE) {
            travel.setReleaseTime(new Date());
        }
        travelMapper.updateState(travel);
    }

    @Override
    public List<Long> queryByUserId(Long currentId) {
        return travelMapper.selectByUserId(currentId);
    }

    @Override
    public List<Travel> queryDetailIdAndSubTitle() {
        return travelMapper.selectIdAndSubTitle();
    }

    @Override
    public List<Travel> getTravelByAuthorId(Long userInfoId) {

        List<Travel> travels = travelMapper.selectTravelByAuthorId(userInfoId);
        return travels;
    }

    @Override
    public PageInfo<Travel> getPageInfoByAuthorId(TravelQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());

        return new PageInfo<Travel>(travelMapper.queryTravelByAuthorId(qo));
    }

    @Override
    public Travel queryDetailById(Long travelId) {
        return travelMapper.selectByPrimaryKey(travelId);
    }

}
